<template>
  <div class="cart-bottom-bar">
    <div class="select-all">
      <check-button :isCheck="isSelect" @click.native="selectAll" />
      <p>全选</p>
    </div>
    <div class="all-price">
      <span>总价:{{allPrice}}</span>
    </div>
    <div class="calculate" @click="calcClick">
      <span>去计算({{goToCalc}})</span>
    </div>
  </div>
</template>

<script>

import CheckButton from "components/content/checkButton/CheckButton";

export default {
  name: "CartBottomBar",
  data(){
    return {
      
    }
  },
  components: {
    CheckButton,
  },
  computed: {
    allPrice() {
      return this.$store.state.cartList
        .filter((item) => item.checked)
        .reduce((pre, cur) => pre + cur.price * cur.count, 0)
        .toFixed(2);
    },
    goToCalc() {
      return this.$store.state.cartList.filter((item) => item.checked).length;
    },
    isSelect(){
      //只要有一个没有选中，那么就不应该让他展示全选
     if(this.$store.state.cartList.length === 0){
       return false
     }else{
       return !this.$store.state.cartList.find(item => !item.checked)
     }
      
    }
  },
  methods: {
    selectAll() {
      if(this.isSelect){
        this.$store.state.cartList.forEach(item => item.checked = false)
      }else{
        this.$store.state.cartList.forEach(item => item.checked = true)
      }
      
    },
    calcClick(){
      const cartList = this.$store.state.cartList

      if((cartList.length === 0 ) || (cartList.every(item => item.checked === false))){
        this.$toast.show('请先选择商品')
      }else{
        this.$toast.show('购买成功')
      }

    }
  },
};
</script>

<style scoped>
.cart-bottom-bar {
  height: 40px;
  background-color: #eee;
  display: flex;
  font-size: 12px;
  text-align: center;
}

.select-all {
  display: flex;
  align-items: center;
  width: 60px;
  margin-left: 10px;
}
.select-all p {
  margin-left: 5px;
}

.all-price {
  display: flex;
  align-items: center;
  flex: 1;
  margin-left: 15px;
}

.calculate {
  width: 90px;
  background-color: red;
  text-align: center;
  line-height: 40px;
  color: #fff;
}
</style>